package com.wbj.direction.sensor

import android.content.Context
import android.hardware.Sensor
import android.hardware.SensorEvent
import android.hardware.SensorEventListener
import android.hardware.SensorManager
import android.util.AttributeSet
import android.util.Log
import android.widget.LinearLayout
import com.wbj.direction.listener.LifecycleListener

/**
 * @auth: WANGCHUANGJUN
 * @mail: wangchuangjun@noboauto.com
 * @desc:
 * @date: 2021/12/13 15:39
 */
abstract class BaseSensorView : LinearLayout, SensorEventListener, LifecycleListener {

    open val TAG = this::class.java.simpleName
    private val mSensorManager: SensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    override fun onResume() {
        mSensorManager.registerListener(
            this,
            mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),
            SensorManager.SENSOR_DELAY_GAME
        )
        Log.i(TAG, "onResume: 回调")
    }

    override fun onPause() {
        mSensorManager.unregisterListener(this)
        Log.i(TAG, "onPause: 回调")
    }

    override  fun onStop() {
        mSensorManager.unregisterListener(this)
        Log.i(TAG, "onStop: 回调")
    }

    override fun onSensorChanged(event: SensorEvent?) {
        Log.i(TAG, "onSensorChanged: 回调")
    }

    override fun onAccuracyChanged(p0: Sensor?, p1: Int) {

    }
}